const database = require("../app/database");
const { APP_PORT, APP_HOST } = require("../app/config");

class UploadServices {
  //上传图片
  async uploadpic(file) {
    const { filename, mimetype, size } = file;
    const str = `
			insert into picture(mimetype, size,filename) values (?,?,?)
		`;
    const [result] = await database.execute(str, [mimetype, size, filename]);

    const url = `http://${APP_HOST}:${APP_PORT}/upload/getpic/${result.insertId}`;
    return {
      status: 200,
      result: url,
    };
  }

  //获取图片
  async getpic(id) {
    const str = `
      select * from picture where id=?
    `;
    const [[result]] = await database.execute(str, [id]);
    return {
      filename: result.filename,
      mimetype: result.mimetype,
    };
  }
}

module.exports = new UploadServices();
